package com.wondertek.service.quartz;

import com.wondertek.common.util.base.RedisUtil;
import com.wondertek.service.entity.sys.SysJobPo;
import org.quartz.CronExpression;

import java.util.Date;

/**
 * 抽象业务定时
 */
public abstract class AbstratBusJob {

    /**
     * 缓存操作
     */
    protected RedisUtil redisUtil;

    /**
     * 执行定时任务
     * @param sysJobPo
     */
    protected abstract void  execute(SysJobPo sysJobPo);


    public void initData(SysJobPo sysJobPo){
        this.redisUtil = RedisUtil.me();
        execute(sysJobPo);
    }

    public boolean isOnlyOnce(String cronExpression){
        try{
            CronExpression cron = new CronExpression(cronExpression);
            Date nextValidTmie = cron.getNextValidTimeAfter(new Date());
            Date seconValidTime = cron.getNextValidTimeAfter(nextValidTmie);
            return seconValidTime == null;
        }catch (Exception e){
            return false;
        }
    }
}
